#include "stm32f10x.h"

u16 msec = 0;

int main() {}

void TIM2_Init() {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

    // 定时1ms
    TIM_TimeBaseInitStructure.TIM_Prescaler = 36000-1;
    TIM_TimeBaseInitStructure.TIM_Period = 2000-1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
    
    TIM_UpdateRequestConfig(TIM2, TIM_UpdateSource_Regular);
    TIM_Cmd(TIM2, ENABLE);
    TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);
}

void NVIC_Configuration() {
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

}

void TIM2_IRQHandler() {
    if (!TIM_GetITStatus(TIM2, TIM_IT_Update)) {
        msec++;
    }
    TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);
}